package com.spark.searchservice.controller;

import com.spark.basecommon.api.R;
import com.spark.searchapi.entity.PmsProduct;
import com.spark.searchservice.service.IEsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author fuqiang
 * @Description elastic搜索服务
 * @Date 2021-03-05
 */
@RestController("/esPro")
@Api(tags = "搜索商品服务")
@AllArgsConstructor
public class EsProductController {

    private IEsProductService esProductService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @RequestMapping(value = "/importAll", method = RequestMethod.POST)
    public R<Integer> importAllList() {
        int count = esProductService.importAll();
        return R.success(count);
    }

    @ApiOperation("根据keywords或者副标题或者名称或者描述简单查询商品")
    @GetMapping("/simpleSearch")
    public R<Page<PmsProduct>> simpleSearch(@RequestParam(required = false) String keywords,
                                            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                            @RequestParam(required = false, defaultValue = "5") Integer pageSize){
        Page<PmsProduct> simplePmsProduct = esProductService.getSimplePmsProduct(keywords, pageNum, pageSize);
        return R.success(simplePmsProduct);
    }

    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public R<Page<PmsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false) Long brandId,
                                                      @RequestParam(required = false) Long productCategoryId,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                      @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<PmsProduct> esProductPage = esProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        return R.success(esProductPage);
    }

}
